import View from "./base/View";
import { EPokerStatus, Esuit } from "./ConfigEnum";
import { Poker } from "./gameView/GameDB";
import GameEvent from "./gameView/GameEvent";
import SplashScene from "./SplashScene";




const {ccclass, property} = cc._decorator;

const POINT_MAP = {
    1: "A",
    2: "2",
    3: "3",
    4: "4",
    5: "5",
    6: "6",
    7: "7",
    8: "8",
    9: "9",
    10: "10",
    11: "J",
    12: "Q",
    13: "K"
}

@ccclass
export default class UIPoker extends cc.Component {
    // [x: string]: any;

    @property(cc.Label)    pointLabel: cc.Label = null;
    @property(cc.Sprite)    smallSuitSprit: cc.Sprite = null;
    @property(cc.Sprite)    bigSuitSprit: cc.Sprite = null;

    @property(cc.Sprite)    bgSprit: cc.Sprite = null;
    @property(cc.SpriteFrame) textFrontBG: cc.SpriteFrame = null;
    @property(cc.SpriteFrame) textBackBG: cc.SpriteFrame = null;
    @property([cc.SpriteFrame]) bigSuits: cc.SpriteFrame[] = [];
    @property([cc.SpriteFrame])    smallSuits: cc.SpriteFrame[] = [];
    @property([cc.SpriteFrame])    texFaces: cc.SpriteFrame[] = [];

    private redTextColor:cc.Color = cc.color(189,33,41);
    private blackTextColor:cc.Color = cc.Color.BLACK;
    private m_Poker:Poker;

    /*********************************************************************************************
     * 生命周期函数
     *********************************************/
    start(): void {
        //注册触摸事件
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
    }
    onDestroy(): void {
        this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
    }
    public Init(poker :Poker){
        poker.Bind(this);
        this.pointLabel.string = `${POINT_MAP[poker.point]}`;
        this.pointLabel.node.color = (poker.suit == Esuit.HEITAO || poker.suit == Esuit.MEIHUA) ? this.blackTextColor : this.redTextColor;
        if (poker.point >= 11){
            this.bigSuitSprit.spriteFrame = this.texFaces[poker.point - 11];
        }else{
            this.bigSuitSprit.spriteFrame = this.bigSuits[poker.suit];
        }
        
        this.smallSuitSprit.spriteFrame = this.smallSuits[poker.suit];
        this.setStatus(poker._status);
        
    }
    Rrfresh() {
        // this.setStatus();
        // throw new Error("Method not implemented.");
        this.bgSprit.spriteFrame = this.textFrontBG;
        this.pointLabel.node.active = true;
        this.smallSuitSprit.node.active = true;
        this.bigSuitSprit.node.active = true;
    }

    private setStatus(status:EPokerStatus)  {
       // return;
        if(status == EPokerStatus.CLOSE){
            this.bgSprit.spriteFrame = this.textBackBG;
            this.pointLabel.node.active = false;
            this.smallSuitSprit.node.active = false;
            this.bigSuitSprit.node.active = false;
        }else{
            this.bgSprit.spriteFrame = this.textFrontBG;
            this.pointLabel.node.active = true;
            this.smallSuitSprit.node.active = true;
            this.bigSuitSprit.node.active = true;
        }
    }

    /***********************************************************************
     * 事件Event handler
     ***********************************************************************/
    onTouchStart(event){
        let x = this.node.convertToNodeSpaceAR(event).x;
        console.log('点击事件开始',x);
    }
    onTouchEnd(event){
        let x = this.node.convertToNodeSpaceAR(event).x;
        SplashScene.eventTarget.emit(GameEvent.CLICK_POKER,this.m_Poker);
    }
    onTouchMove(event){
        let x = this.node.convertToNodeSpaceAR(event).x;
        console.log('点击事件移动',x);
    }
    onTouchCancel(event){
        let x = this.node.convertToNodeSpaceAR(event).x;
        // this.emit(GameEvent.CLICK_POKER,this.m_Poker)
        console.log('点击事件取消',x);
    }
}